package crawler.client.sdk.expand.ip;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import crawler.utility.http.HttpClientResponse;
import crawler.utility.http.HttpClientUtil;


public class GetIPAddress {
	
	private static Logger log = LoggerFactory.getLogger(GetIPAddress.class);
	
	private static final String URL = "http://1212.ip138.com/ic.asp";
	
	public static String getClientIp() {
		String ip = null;
		HttpClientUtil httpClientUtil = new HttpClientUtil();
		httpClientUtil.setUrl(URL);
		httpClientUtil.setCharset(HttpClientUtil.GB2312);
		
		try {
			HttpClientResponse responseBean = httpClientUtil.get();
			
			if (responseBean != null) {
				String html = responseBean.getContent();
				log.info("GetIPAddress --- getClientIp  html" + html);
				
				Document doc = Jsoup.parse(html);
				String center = doc.select("center").first().text();
				
				log.info("GetIPAddress --- getClientIp  center : " + center);
				
				ip = center.substring(center.indexOf("[") + 1, center.indexOf("]"));
				
				log.info("GetIPAddress --- getClientIp  ip : " + ip);
			}
			
		} catch (IOException e) {
			log.error("Get IP Error",e);
			e.printStackTrace();
		}
		
		return ip;
	}
	
	
	
	public static void main(String[] args) throws ClientProtocolException, IOException {
		getClientIp();
	}
}
